<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>题库分类设置</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=312"/>
    <style>
        .layui-form-item {
            margin-bottom: 25px;
        }

        label .layui-icon {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>

<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <!-- 右表 -->
        <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
            <div class="layui-card">
                <div class="layui-card-body" style="min-height: 535px;">
                    <blockquote class="layui-elem-quote">注意： 添加，修改分类！别名均不允许重复!</blockquote>
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" id="btn_add">
                                    <i class="layui-icon">&#xe654;</i>添加分类
                                </button>
                            </div>
                        </div>
                    </div>
                    <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
                    <br>
                    <blockquote class="layui-elem-quote">注意： 一级分类如果菜单为不显示，则二级分类不会显示 (默认添加为显示，可自行修改)</blockquote>
                </div>
            </div>
        </div>

    </div>

</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarTbBas">
    <a class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm" lay-event="del">删除</a>
</script>
<!-- js部分 -->
<script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/assets/js/common.js?v=312"></script>
<script>
    layui.use(['layer', 'jquery', 'admin', 'dropdown', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var admin = layui.admin;
        var dropdown = layui.dropdown;
        var treeTable = layui.treeTable;

        var insTb = treeTable.render({
            elem: '#tableTbBas',
            tree: {
                iconIndex: 0,  // 折叠图标显示在第几列
                idName: 'Id',  // 自定义id字段的名称
                pidName: 'fid',  // 自定义标识是否还有子节点的字段名称
                isPidData: true,  // 是否是pid形式数据
                openName: 'Id'
            },
            cellMinWidth: 100,
            cols: [
                {field: 'Id', align: 'center', sort: true, title: 'ID', width: 90},
                {field: 'name', align: 'center', title: '分类名称'},
                {field: 'alias', align: 'center', title: '分类别名', width: 130},
                {field: 'key', align: 'center', title: '分类关键词'},
                {field: 'desc', align: 'center', title: '分类介绍'},
                {
                    field: 'type', align: 'center', title: '类型', templet: function (d) {
                        if (d.type == 0) {
                            return '<span class="text-primary">默认规则</span>';
                        }
                        if (d.type == 1) {
                            return '<span class="text-danger">完全免费</span>';
                        }
                    }
                },
                {
                    field: 'is_menu', align: 'center', title: '菜单',width:90, templet: function (d) {
                        if (d.is_menu == 0) {
                            return '<span class="text-primary">不显示</span>';
                        }
                        if (d.is_menu == 1) {
                            return '<span class="text-success">显示</span>';
                        }
                    }
                },
                {
                    field: 'fid', align: 'center', title: '上级', width: 80, templet: function (d) {
                        if (d.fid == 0) {
                            return '<span class="layui-badge">顶</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-blue">子</span>';
                        }
                    }
                },
                {align: 'center', toolbar: '#tableBarTbBas', title: '操作', width: 150}
            ],
            reqData: function (data, callback) {
                $.get("{:url('admin/question/sort',['type'=>'get'])}", function (res) {
                    callback(res.data);
                });
            }
        });
        //编辑排序监听
        treeTable.on('edit(tableTbBas)', function (obj) {
            var value = obj.value,
                data = obj.data;
            $.post('{:url("admin/question/sort")}', {
                'id': data.Id,
                'value': value,
                'type': 'listorder'
            }, function (data) {
                if (data.code == 1) {
                    layer.msg(data.msg);
                } else {
                    layer.msg(data.msg);
                }
            })
        });
        //监听工具条
        treeTable.on('tool(tableTbBas)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                if (data.fid == 0) {
                    layer.open({
                        type: 2,
                        area: '500px',
                        title: '修改分类',
                        content: "{:url('admin/question/sort_edit')}?id=" + data.Id,
                        end: function () {
                            insTb.reload();
                        }
                    });
                } else {
                    layer.open({
                        type: 2,
                        area: '500px',
                        title: '修改分类',
                        content: "{:url('admin/question/sort_edit_c')}?id=" + data.Id,
                        end: function () {
                            insTb.reload();
                        }
                    });
                }

            } else if (layEvent === 'del') { // 删除
                layer.open({
                    title: '删除确认'
                    , content: '将要分类：' + data.name + ' 删除吗？'
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        $.ajax({
                            type: "POST",
                            url: "{:url('admin/question/sort')}",
                            data: 'type=del&id=' + data.Id,
                            dataType: "json",
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.msg('删除成功', {
                                        icon: 1,
                                        time: 2000
                                    }, function () {
                                        obj.del();
                                    });
                                } else if (data.code == -1) {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                alert("请求失败");
                            }
                        });
                        layer.closeAll();
                    }
                });

            }
            dropdown.hideAll();
        });

        $('#btn_add').click(function () {
            layer.open({
                type: 2,
                area: ['50%', '50%'],
                title: '添加分类',
                content: "{:url('admin/question/sort_add')}",
                end: function () {
                    insTb.reload();
                }
            });
        });
        $('#btn_alldel').click(function () {
            layer.open({
                title: '删除确认'
                , content: '将要删除全部分类内容！确定吗？'
                , btn: ['确认', '取消']
                , yes: function (index, layero) {
                    $.ajax({
                        type: "POST",                      //请求类型
                        url: "{:url('admin/question/sort')}",//URL
                        data: 'type=delall',   //传递的参数
                        dataType: "json",                 //返回的数据类型
                        success: function (data) {
                            if (data.code == 0) {
                                layer.alert('清空成功！');
                                insTb.reload();
                            } else if (data.code == -1) {
                                layer.alert(data.msg);
                            }
                        },
                        error: function (data) {
                            alert("请求失败");
                        }
                    });
                    layer.closeAll();
                }
            });
        });

    });
</script>
</body>

</html>